package com.example.biyesheji.service.impl;

import com.clearspring.analytics.util.Lists;
import com.example.biyesheji.mapper.ProductMapper;
import com.example.biyesheji.model.result.MenuTableResult;
import com.example.biyesheji.model.result.ProductSaleResult;
import com.example.biyesheji.pojo.ProductSales;
import com.example.biyesheji.service.MenuTableService;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class MenuTableServiceImpl implements MenuTableService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private JavaSparkContext sc;

    @Override
    public List<MenuTableResult> getMenuTableResult() {
        //获取数据
        List<ProductSaleResult> menuTable = productMapper.getMenuTable();
        //取出前7名 防止出现空字符
        List<ProductSaleResult> productSaleResults = menuTable.subList(0, 8);
        //取出前四名获取最近一年数据
        List<String> brandList = new ArrayList<>();
        for (ProductSaleResult pr:productSaleResults
             ) {
            if (pr.getBrand() !=null && pr.getBrand() !=""){
                brandList.add(pr.getBrand());
            }
        }
        //取出4个季度
        List<String> newbrandList = brandList.subList(0,5);

        List<String> list = productMapper.itemList(newbrandList.get(0)).subList(0,99);
        List<String> list1 = productMapper.itemList(newbrandList.get(1)).subList(0,99);
        List<String> list2 = productMapper.itemList(newbrandList.get(2)).subList(0,99);
        List<String> list3 = productMapper.itemList(newbrandList.get(3)).subList(0,99);

        MenuTableResult result = new MenuTableResult();

        List<MenuTableResult> results = new ArrayList<>();

        MenuTableResult one = new MenuTableResult();
        one.setBrandName(newbrandList.get(0));
        one.setBrandSale(list);

        MenuTableResult two = new MenuTableResult();
        two.setBrandName(newbrandList.get(1));
        two.setBrandSale(list1);

        MenuTableResult three = new MenuTableResult();
        three.setBrandName(newbrandList.get(2));
        three.setBrandSale(list2);

        MenuTableResult four = new MenuTableResult();
        four.setBrandName(newbrandList.get(3));
        four.setBrandSale(list3);
        List<String> list4 = productMapper.timeList().subList(0, 99);

        one.setXList(list4);
        one.setBrandList(brandList.subList(0,4));

        results.add(one);results.add(two);results.add(three);results.add(four);

        return results;
    }

    @Override
    public List<ArrayList> brandPie() {
        //返回数据
        List<ArrayList> dataList =new ArrayList<>();

        //读取四季度数据
        List<Integer> bejinone = new ArrayList<>(Arrays.asList(0,0))  ;  //品牌一第一季度
        List<Integer> beijntwo = new ArrayList<>(Arrays.asList(0,0));   // 季度2
        List<Integer> beijinthree = new ArrayList<>(Arrays.asList(0,0));  //3季度
        List<Integer> beijinfour = new ArrayList<>(Arrays.asList(0,0));   //4季度

        List<Integer> shanghainone = new ArrayList<>(Arrays.asList(0,0)) ;  //品牌二第一季度
        List<Integer> shanghaitwo = new ArrayList<>(Arrays.asList(0,0));   // 季度2
        List<Integer> shanghaithree = new ArrayList<>(Arrays.asList(0,0));  //3季度
        List<Integer> shanghaifour = new ArrayList<>(Arrays.asList(0,0));   //4季度

        List<Integer> guangzhouone = new ArrayList<>(Arrays.asList(0,0)) ;  //品牌三第一季度
        List<Integer> guangzhoutwo = new ArrayList<>(Arrays.asList(0,0));   // 季度2
        List<Integer> guangzhouthree = new ArrayList<>(Arrays.asList(0,0));  //3季度
        List<Integer> guangzhoufour = new ArrayList<>(Arrays.asList(0,0));   //4季度


        List<Integer> tuangzhouone = new ArrayList<>(Arrays.asList(0,0)) ;  //品牌四第一季度
        List<Integer> tuangzhoutwo = new ArrayList<>(Arrays.asList(0,0));   // 季度2
        List<Integer> tuangzhouthree = new ArrayList<>(Arrays.asList(0,0));  //3季度
        List<Integer> tuangzhoufour = new ArrayList<>(Arrays.asList(0,0));   //4季度

        //获取品牌列表
        List<String> brandList = productMapper.getBrandList();
        for (int i=0;i<brandList.size();i++){
            if (brandList == null || brandList.equals("null")){
                brandList.remove(i);
            }
        }
        System.out.println(brandList);
        List<ProductSales> brandEBP = productMapper.getBrandEBP();
        //去空
        for (ProductSales productSales:brandEBP) {
            if(productSales.getBrand() != null && productSales.getEventTime() != null) {
                int i = Integer.parseInt(productSales.getEventTime().substring(5, 7));
                Float f = productSales.getPrice();
                String s = productSales.getBrand();
                //System.out.println(s);

                if (s.equals(brandList.get(1)) || s == brandList.get(1)) {

                    if (1 <= i && i < 4) {
                        bejinone.add(Math.round(f));
                    }
                    if (4 <= i && i < 7) {
                        beijntwo.add(Math.round(f));
                    }
                    if (7 <= i && i < 10) {
                        beijinthree.add(Math.round(f));
                    }
                    if (10 <= i && i < 13) {
                        beijinfour.add(Math.round(f));
                    }
                }

                if (s.equals(brandList.get(4)) || s == brandList.get(4)) {
                    if (1 <= i && i < 4) {
                        shanghainone.add(Math.round(f));
                    }
                    if (4 <= i && i < 7) {
                        shanghaitwo.add(Math.round(f));
                    }
                    if (7 <= i && i < 10) {
                        shanghaithree.add(Math.round(f));
                    }
                    if (10 <= i && i < 13) {
                        shanghaifour.add(Math.round(f));
                    }
                }
                if (s.equals(brandList.get(3)) || s == brandList.get(3)) {
                    if (1 <= i && i < 4) {
                        guangzhouone.add(Math.round(f));
                    }
                    if (4 <= i && i < 7) {
                        guangzhoutwo.add(Math.round(f));
                    }
                    if (7 <= i && i < 10) {
                        guangzhouthree.add(Math.round(f));
                    }
                    if (10 <= i && i < 13) {
                        guangzhoufour.add(Math.round(f));
                    }
                }
                if (s.equals(brandList.get(5)) || s == brandList.get(5)) {
                    if (1 <= i && i < 4) {
                        tuangzhouone.add(Math.round(f));
                    }
                    if (4 <= i && i < 7) {
                        tuangzhoutwo.add(Math.round(f));
                    }
                    if (7 <= i && i < 10) {
                        tuangzhouthree.add(Math.round(f));
                    }
                    if (10 <= i && i < 13) {
                        tuangzhoufour.add(Math.round(f));
                    }
                }

            }
        }


        //rdd
        JavaRDD<Integer> brdd = sc.parallelize(bejinone);
        JavaRDD<Integer> brdd1 = sc.parallelize(beijntwo);
        JavaRDD<Integer> brdd2 = sc.parallelize(beijinthree);
        JavaRDD<Integer> brdd3 = sc.parallelize(beijinfour);

        JavaRDD<Integer> rdds = sc.parallelize(shanghainone);
        JavaRDD<Integer> rdd1s = sc.parallelize(shanghaitwo);
        JavaRDD<Integer> rdd2s = sc.parallelize(shanghaithree);
        JavaRDD<Integer> rdd3s = sc.parallelize(shanghaifour);

        JavaRDD<Integer> rddg = sc.parallelize(guangzhouone);
        JavaRDD<Integer> rdd1g = sc.parallelize(guangzhoutwo);
        JavaRDD<Integer> rdd2g = sc.parallelize(guangzhouthree);
        JavaRDD<Integer> rdd3g = sc.parallelize(guangzhoufour);

        JavaRDD<Integer> rddt = sc.parallelize(tuangzhouone);
        JavaRDD<Integer> rdd1t = sc.parallelize(tuangzhoutwo);
        JavaRDD<Integer> rdd2t = sc.parallelize(tuangzhouthree);
        JavaRDD<Integer> rdd3t = sc.parallelize(tuangzhoufour);

        Integer breduce = brdd.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);   //一季度
        Integer breduce1 = brdd1.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce2 = brdd2.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce3 = brdd3.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);  //四季度



        Integer breduces = rdds.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);   //一季度
        Integer breduce1s = rdd1s.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce2s = rdd2s.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce3s = rdd3s.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);  //四季度

        System.out.println(breduces+"-------------"+ breduce1s+"--------------"+breduce3s);

        Integer breduceg = rddg.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);   //一季度
        Integer breduce1g = rdd1g.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce2g = rdd2g.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce3g = rdd3g.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);  //四季度

        Integer breducet = rddt.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);   //一季度
        Integer breduce1t = rdd1t.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce2t = rdd2t.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer breduce3t = rdd3t.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);  //四季度

        ArrayList list1 = new ArrayList<>();
        ArrayList list2 = new ArrayList<>();
        ArrayList list3 = new ArrayList<>();
        ArrayList list4 = new ArrayList<>();
        //品牌一
        list1.add(0,brandList.get(0));list1.add(1,breduce);list1.add(2,breduce1);list1.add(3,breduce2);list1.add(4,breduce3);
        list2.add(0,brandList.get(4));list2.add(1,breduces);list2.add(2,breduce1s);list2.add(3,breduce2s);list2.add(4,breduce3s);
        list3.add(0,brandList.get(1));list3.add(1,breduceg);list3.add(2,breduce1g);list3.add(3,breduce2g);list3.add(4,breduce3g);
        list4.add(0,brandList.get(3));list4.add(1,breducet);list4.add(2,breduce1t);list4.add(3,breduce2t);list4.add(4,breduce3t);

        ArrayList<String> saa = new ArrayList<>(); saa.add("product");saa.add("第一季度"); saa.add("第二季度"); saa.add("第三季度");saa.add("第四季度");
        dataList.add(0,saa);
        dataList.add(1,list1);dataList.add(2,list2);dataList.add(3,list3);dataList.add(4,list4);



        //消除
        brdd.unpersist();
        brdd1.unpersist();
        brdd2.unpersist();
        brdd3.unpersist();

        rdds.unpersist();
        rdd1s.unpersist();
        rdd2s.unpersist();
        rdd3s.unpersist();


        rddg.unpersist();
        rdd1g.unpersist();
        rdd2g.unpersist();
        rdd3g.unpersist();

        rdd1t.unpersist();
        rdd2t.unpersist();
        rdd3t.unpersist();
        rddt.unpersist();


        return dataList;

    }
}
